Add `ENABLE_ASSERTIONS` build option
authorAntonio Nino Diaz <[email protected]>
Tue, 18 Apr 2017 14:16:05 +0000 (15:16 +0100)
committerAntonio Nino Diaz <[email protected]>
Wed, 19 Apr 2017 15:00:53 +0000 (16:00 +0100)
Add the new build option `ENABLE_ASSERTIONS` that controls whether or
not assert functions are compiled out. It defaults to 1 for debug builds
and to 0 for release builds.

Additionally, a following patch will be done to allow this build option
to hide auxiliary code used for the checks done in an `assert()`. This
code is is currently under the DEBUG build flag.

Assert messages are now only printed if LOG_LEVEL >= LOG_LEVEL_INFO,
which is the default for debug builds.

This patch also updates the User Guide.

Change-Id: I1401530b56bab25561bb0f274529f1d12c5263bc
Signed-off-by: Antonio Nino Diaz <[email protected]>
Makefile
common/aarch32/debug.S
common/aarch64/debug.S
docs/user-guide.md
include/lib/stdlib/assert.h
lib/stdlib/assert.c

index 02aa50e3d87dcccb36d462b8c13da35aab1b93bd..d03c2bf24d4d64ee0d45ca7ba1159e48af7e42f4 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -52,8 +52,9 @@ include ${MAKE_HELPERS_DIRECTORY}build_env.mk
 
 include ${MAKE_HELPERS_DIRECTORY}defaults.mk
 
-# ASM_ASSERTION enabled for DEBUG builds only
+# Assertions enabled for DEBUG builds by default
 ASM_ASSERTION                  := ${DEBUG}
+ENABLE_ASSERTIONS              := ${DEBUG}
 ENABLE_PMF                     := ${ENABLE_RUNTIME_INSTRUMENTATION}
 PLAT                           := ${DEFAULT_PLAT}
 
@@ -446,6 +447,7 @@ $(eval $(call assert_boolean,CTX_INCLUDE_AARCH32_REGS))
 $(eval $(call assert_boolean,CTX_INCLUDE_FPREGS))
 $(eval $(call assert_boolean,DEBUG))
 $(eval $(call assert_boolean,DISABLE_PEDANTIC))
+$(eval $(call assert_boolean,ENABLE_ASSERTIONS))
 $(eval $(call assert_boolean,ENABLE_PLAT_COMPAT))
 $(eval $(call assert_boolean,ENABLE_PMF))
 $(eval $(call assert_boolean,ENABLE_PSCI_STAT))
@@ -482,6 +484,7 @@ $(eval $(call add_define,ASM_ASSERTION))
 $(eval $(call add_define,COLD_BOOT_SINGLE_CPU))
 $(eval $(call add_define,CTX_INCLUDE_AARCH32_REGS))
 $(eval $(call add_define,CTX_INCLUDE_FPREGS))
+$(eval $(call add_define,ENABLE_ASSERTIONS))
 $(eval $(call add_define,ENABLE_PLAT_COMPAT))
 $(eval $(call add_define,ENABLE_PMF))
 $(eval $(call add_define,ENABLE_PSCI_STAT))
index 77298a12e6aa07247033f4f015dc0b7383c64fb1..bafce7cef8366424fb87c3ed4302b6ede792828b 100644 (file)
@@ -107,6 +107,11 @@ assert_msg2:
  * ---------------------------------------------------------------------------
  */
 func asm_assert
+#if LOG_LEVEL >= LOG_LEVEL_INFO
+       /*
+        * Only print the output if LOG_LEVEL is higher or equal to
+        * LOG_LEVEL_INFO, which is the default value for builds with DEBUG=1.
+        */
        /* Stash the parameters already in r0 and r1 */
        mov     r5, r0
        mov     r6, r1
@@ -147,6 +152,7 @@ dec_print_loop:
        bl      plat_crash_console_flush
 
 1:
+#endif /* LOG_LEVEL >= LOG_LEVEL_INFO */
        no_ret  plat_panic_handler
 endfunc asm_assert
 #endif
index fb6924ea19c75de065faffbfeded7a172ce9bc6b..3d8e0f413fc3d0841690effee42e210c0ff0c9a7 100644 (file)
@@ -78,6 +78,11 @@ dec_print_loop:
  * ---------------------------------------------------------------------------
  */
 func asm_assert
+#if LOG_LEVEL >= LOG_LEVEL_INFO
+       /*
+        * Only print the output if LOG_LEVEL is higher or equal to
+        * LOG_LEVEL_INFO, which is the default value for builds with DEBUG=1.
+        */
        mov     x5, x0
        mov     x6, x1
        /* Ensure the console is initialized */
@@ -98,6 +103,7 @@ func asm_assert
        asm_print_line_dec
        bl      plat_crash_console_flush
 _assert_loop:
+#endif /* LOG_LEVEL >= LOG_LEVEL_INFO */
        no_ret  plat_panic_handler
 endfunc asm_assert
 #endif
index a1df96524b985c9e5f9831c3165351de37dbaf1d..af083605c794f219b067e454c5a12d31e793040e 100644 (file)
@@ -286,6 +286,14 @@ performed.
     payload. Please refer to the "Booting an EL3 payload" section for more
     details.
 
+*   `ENABLE_ASSERTIONS`: This option controls whether or not calls to `assert()`
+    are compiled out. For debug builds, this option defaults to 1, and calls to
+    `assert()` are left in place. For release builds, this option defaults to 0
+    and calls to `assert()` function are compiled out. This option can be set
+    independently of `DEBUG`. It can also be used to hide any auxiliary code
+    that is only required for the assertion and does not fit in the assertion
+    itself.
+
 *   `ENABLE_PMF`: Boolean option to enable support for optional Performance
      Measurement Framework(PMF). Default is 0.
 
index 5621f8cac7e2c2e13456189d3a9639e0cbd965cb..1bcd1ead5e6be22045c91a387221e345d54e8103 100644 (file)
  *     @(#)assert.h    8.2 (Berkeley) 1/21/94
  * $FreeBSD$
  */
-
-#include <sys/cdefs.h>
-
 /*
- * Unlike other ANSI header files, <assert.h> may usefully be included
- * multiple times, with and without NDEBUG defined.
+ * Portions copyright (c) 2017, ARM Limited and Contributors.
+ * All rights reserved.
  */
 
-#undef assert
-#undef _assert
+#ifndef _ASSERT_H_
+#define _ASSERT_H_
 
-#ifdef NDEBUG
-#define        assert(e)       ((void)0)
-#define        _assert(e)      ((void)0)
-#else
-#define        _assert(e)      assert(e)
+#include <sys/cdefs.h>
 
+#if ENABLE_ASSERTIONS
+#define        _assert(e)      assert(e)
 #define        assert(e)       ((e) ? (void)0 : __assert(__func__, __FILE__, \
                            __LINE__, #e))
-#endif /* NDEBUG */
+#else
+#define        assert(e)       ((void)0)
+#define        _assert(e)      ((void)0)
+#endif /* ENABLE_ASSERTIONS */
 
-#ifndef _ASSERT_H_
-#define _ASSERT_H_
 __BEGIN_DECLS
 void __assert(const char *, const char *, int, const char *) __dead2;
 __END_DECLS
+
 #endif /* !_ASSERT_H_ */
index 3486e50b1549f077868734c44a1f8b2b006a61dd..3c0bd166f17a24ec89a0d4bd6558afdc65ae06e2 100644 (file)
 #include <debug.h>
 #include <platform.h>
 
-/*
- * This is a basic implementation. This could be improved.
- */
-void __assert (const char *function, const char *file, unsigned int line,
+void __assert(const char *function, const char *file, unsigned int line,
                const char *assertion)
 {
+#if LOG_LEVEL >= LOG_LEVEL_INFO
+       /*
+        * Only print the output if LOG_LEVEL is higher or equal to
+        * LOG_LEVEL_INFO, which is the default value for builds with DEBUG=1.
+        */
        tf_printf("ASSERT: %s <%d> : %s\n", function, line, assertion);
 
        console_flush();
+#endif
 
        plat_panic_handler();
 }